package com.lovebridge.ui;

import com.jiehoo.ooqi.SearchResult;
import com.lovebridge.model.Base;
import com.lovebridge.service.IService;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.ext.AfterCompose;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import java.util.HashMap;
import java.util.Map;

/**
 * BaseListWindow.
 */
public abstract class BaseListWindow extends BaseWindow implements AfterCompose, IService
{
    private Base current = newObject();

    public BaseListWindow()
    {
        addListName(getDefaultListName());
    }

    protected Base newObject()
    {
        return new Base();
    }

    private String getEditFormName()
    {
        return getDefaultListName() + "Form.zul";
    }

    protected void openEditFormWindow() throws InterruptedException
    {
        HashMap<String,Object> params = new HashMap<String,Object>();
        params.put(getDefaultListName(), getCurrent());
        openModalWindow(params, getEditFormName());
    }

    protected void openModalWindow(Map params, String pageName) throws InterruptedException
    {
        Sessions.getCurrent().setAttribute(Constants.PARENT_LIST_WINDOW,this);
        Window window = (Window) Executions.createComponents(pageName, this, params);
        window.doHighlighted();
    }

    public void onAdd()
    {
        setCurrent(newObject());
        try
        {
            openEditFormWindow();
        }
        catch (Throwable t)
        {
            handleException(t);
        }
    }

    public void onEdit()
    {
        try
        {
            if (current == null || current.getId() == 0)
            {
                Messagebox.show("Please select one " + getDefaultListName() + ".");
                return;
            }
            openEditFormWindow();
        }
        catch (Throwable t)
        {
            handleException(t);
        }
    }

    public void onDelete()
    {
        try
        {
            service.delete(getCurrent());
            refreshList(getDefaultListName());
        }
        catch (Throwable t)
        {
            handleException(t);
        }
    }

    public void onSave()
    {
        try
        {
            if (getCurrent().getId() != 0)
            {
                service.update(getCurrent());
            }
            else
            {
                service.create(getCurrent());
            }
            refreshList(getDefaultListName());
        }
        catch (Throwable t)
        {
            handleException(t);
        }
    }

    public Base getCurrent()
    {
        return current;
    }

    public void setCurrent(Base current)
    {
        this.current = current;
    }

    public SearchResult query(String name) throws Exception
    {
        throw new RuntimeException("Unknow query target: " + name);
    }

    public void delete(Base object) throws Exception
    {
    }

    public void create(Base object) throws Exception
    {
    }

    public void update(Base object) throws Exception
    {
    }
}
